import json
import urllib.request
import ssl

import jsonpath

ssl._create_default_https_context = ssl._create_unverified_context
url = 'https://dianying.taobao.com/cityAction.json?activityId&_ksTS=1686920381956_108&jsoncallback=jsonp109&action=cityAction&n_s=new&event_submit_doGetAllRegion=true'

headers = {
    "Accept":
        "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01",
    # "Accept-Encoding": "gzip, deflate, br",  # 一定要注释掉
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Bx-V":
        "2.5.0",
    "Cookie":
        "cna=EWXIHMcvJhEBASQOA6LcN/t4; t=24cac60f44a82717c8284ae92948c0c2; cookie2=16747b851a1e835c06a60b5d2e16e4bc; v=0; _tb_token_=7737b8b8eee36; xlly_s=1; tfstk=cl5hBixkAhibQMVRfMOQOj758OeAZbuwvgSC_zDGXTcVIifNi_MZ3h6zIhqbYY1..; l=fBjONpfmNy2lBtQvBO5BPurza77OmIRb8lVzaNbMiIEGa6gFtFwbkNC19K3vSdtxgT5ASetPl_pr6dEW5PaLSNsWHpfuKtyuJtJ68eM3N7AN.; isg=BJiYNHSXSPdSaGSRg0ecrTfPacAqgfwLktAVaNKJa1OGbTlXepPWmzFDpKXdxrTj",
    "Referer":
        "https://dianying.taobao.com/",
    "Sec-Ch-Ua":
        '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
    "Sec-Ch-Ua-Mobile":
        "?0",
    "Sec-Ch-Ua-Platform":
        "macOS",
    "Sec-Fetch-Dest":
        "empty",
    "Sec-Fetch-Mode":
        "cors",
    "Sec-Fetch-Site":
        "same-origin",
    "User-Agent":
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
    "X-Requested-With":
        "XMLHttpRequest"
}

request = urllib.request.Request(url=url, headers=headers)

response = urllib.request.urlopen(request)

content = response.read().decode('utf-8')

# 切片
content = content[13:-2:]

# 保存到本地
with open('淘票票json数据.json', 'w', encoding='utf-8') as fs:
    fs.write(content)

val = json.load(open('淘票票json数据.json', 'r', encoding='utf-8'))
list = jsonpath.jsonpath(val, '$..regionName')
print(list)